package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppMessageRemindVo;
import org.dromara.system.domain.bo.AppMessageRemindBo;
import org.dromara.system.service.IAppMessageRemindService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 消息提醒
 *
 * @author Lion Li
 * @date 2025-09-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/messageRemind")
public class AppMessageRemindController extends BaseController {

    private final IAppMessageRemindService appMessageRemindService;

    /**
     * 已读消息
     */
    @RepeatSubmit()
    @PostMapping("/read")
    public R<Void> readEd(@Param("id") Long id) {
        return toAjax(appMessageRemindService.read(id));
    }


    /**
     * 查询消息提醒列表
     */
    @SaCheckPermission("system:messageRemind:list")
    @GetMapping("/list")
    public TableDataInfo<AppMessageRemindVo> list(@RequestHeader("roleid") String roleId, AppMessageRemindBo bo, PageQuery pageQuery) {
        bo.setRoleId(Integer.parseInt(roleId));
        return appMessageRemindService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出消息提醒列表
     */
    @SaCheckPermission("system:messageRemind:export")
    @Log(title = "消息提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppMessageRemindBo bo, HttpServletResponse response) {
        List<AppMessageRemindVo> list = appMessageRemindService.queryList(bo);
        ExcelUtil.exportExcel(list, "消息提醒", AppMessageRemindVo.class, response);
    }

    /**
     * 获取 消息提醒详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:messageRemind:query")
    @GetMapping("/{id}")
    public R<AppMessageRemindVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appMessageRemindService.queryById(id));
    }

    /**
     * 新增消息提醒
     */
    @SaCheckPermission("system:messageRemind:add")
    @Log(title = "消息提醒", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppMessageRemindBo bo) {
        return toAjax(appMessageRemindService.insertByBo(bo));
    }

    /**
     * 修改消息提醒
     */
    @SaCheckPermission("system:messageRemind:edit")
    @Log(title = "消息提醒", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppMessageRemindBo bo) {
        return toAjax(appMessageRemindService.updateByBo(bo));
    }

    /**
     * 删除消息提醒
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:messageRemind:remove")
    @Log(title = "消息提醒", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appMessageRemindService.deleteWithValidByIds(List.of(ids), true));
    }
}
